package com.itluma.isWOak.controller;

import com.github.pagehelper.PageInfo;
import com.itluma.isWOak.pojo.Account;
import com.itluma.isWOak.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: isWOak
 * @author: 小陆
 * @create: 2021-04-07 19:10
 **/

@Controller
@RequestMapping("/account")
public class AccountController {

	@Autowired
	private AccountService accountService;

	/**
	 * @param page:             jsp传入的页码
	 * @param pageNum:每页的大小(固定)
	 * @功能逻辑: 显示账务账号模块中的列表展示和分页
	 * @作者: 小陆
	 * @创建日期:2021/4/7
	 * @方法名称:accountList
	 * @参数:* @param model:返回前端的参数
	 * @返回值:java.lang.String
	 */
	@RequestMapping("/account_list")
	public String accountList(Model model, Integer page, Integer pageNum) {
		System.out.println("------" + page);
		System.out.println("======" + pageNum);
		if (page == null) {
			page = 1;
		}
		if (pageNum == null) {
			pageNum = 10;
		}
		//调用accountservice中的showList方法传入page,pagenum,返回pageInfo对象
		PageInfo<Account> accounts = accountService.showList(page, pageNum);
		if (accounts != null) {
			model.addAttribute("accountList", accounts);
		} else {
			model.addAttribute("accountError", "暂无数据!!");
		}
		return "account/account_list";
	}


	@RequestMapping("/account_detail")
	public String accountList(Model model, Integer accountId) {
		Account account = accountService.showOneUserDetail(accountId);
		if (account != null) {
			model.addAttribute("accountDetail", account);
		} else {
			model.addAttribute("accountError", "此账号暂无数据");
		}
		return "account/account_detail";
	}

	@RequestMapping("/account_add")
	public String accountAddJSP() {
		return "account/account_add";
	}

	@PostMapping("/account_addDetail")
	public String addaccountDetail(Model model, Account account) {
		Date date = new Date();
		System.out.println("date:----" + date);
		String dateFormat = new SimpleDateFormat().format(date);
		System.out.println("dateFormat:=====" + dateFormat);
		System.out.println(date);
		account.setStatus("0");
		account.setRecommendreId(10);
		account.setCreateDate(date);
		System.out.println(account.toString());
		Account insertAccount = accountService.insertAccount(account);
		if (insertAccount != null) {
			model.addAttribute("account", account);
			model.addAttribute("insertSuccess", "添加成功!!!");
			System.out.println("成功!!!!");
			return "account/account_add";
		}
		model.addAttribute("account", account);
		model.addAttribute("insertError", "添加失败!!!");
		System.out.println("失败!!!!!");
		return "account/account_add";
	}

	@PostMapping("/accountIdcordNo")
	@ResponseBody
	public Map<String, Object> idcordNo(String IdcordNo, HttpServletRequest request) {
		String IdcordNO = request.getParameter("IdcordNo");
		System.out.println("IdcordNo====::" + IdcordNO);
		boolean b = accountService.selectByIdcordNo(IdcordNO);
		Map<String, Object> resultMap = new HashMap<>();
		String msg = "可用";
		if (!b) {
			msg = "身份证号已被注册过";
			resultMap.put("code", 1001);
			resultMap.put("data", msg);
//			System.out.println(msg);
			return resultMap;
		}
		resultMap.put("code", 0);
		resultMap.put("data", msg);
//		System.out.println(msg);
		return resultMap;
	}

	@RequestMapping("/account_select")
	public String selectStatus(String status) {
		System.out.println(status);
		return "ok";

	}

	//开通或者暂停
	@RequestMapping("/account_update")
	@ResponseBody
	public String updateStatus(String status, Integer accountId) {
		System.out.println(status);
		System.out.println(accountId);
		boolean b = accountService.updateStatus(status, accountId);
		if (b) {
			return "ok";
		}
		return "unOk";
	}

	@RequestMapping("/account_updateAccount")
	public String updateAccount(Integer accountId, Model model) {
		Account account = accountService.updateAccount(accountId);
		model.addAttribute("account", account);
		return "account/account_modi";
	}

	@PostMapping("/account_modi")
	public String updateAccountModi(Account account, Model model) {
		System.out.println("account ~~~~~~~~ :" + account.toString());
		boolean b = accountService.updateAccountDetail(account);
		if (b) {
//			model.addAttribute("account",account)
			model.addAttribute("success", "修改成功");
			return "account/updateSuccess";
		}
		return "account/updateError";
	}

	@PostMapping("/accountpwd")
	@ResponseBody
	public Map<String, Object> selectPwd(String oldpwd, Integer accountId) {
		System.out.println(oldpwd);
		System.out.println(accountId);
		boolean b = accountService.selectPwd(oldpwd, accountId);
		Map<String, Object> resultMap = new HashMap<>();
		if (b) {
			resultMap.put("code", 0);
		} else {
			resultMap.put("code", 1001);
		}
		return resultMap;
	}

	@RequestMapping("/account_deleteAccount")
	public String deleteAccount(Integer accountId){
		System.out.println(accountId);
		boolean b =  accountService.deleteAccount(accountId);
		if (b) {
			return "account/account_list";
		}
		return "ok";
	}


}
